#include <STC15F2K60S2.H>

#define SEGOFF 0xFF

typedef unsigned int uint ;
typedef unsigned char uchar ;


//seg
uchar segTable[] ={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
uchar segDisp[9] ={SEGOFF,SEGOFF,SEGOFF,SEGOFF,SEGOFF,SEGOFF,SEGOFF,SEGOFF,SEGOFF};

//led
uchar LedDisp;

//key
uchar keyCount;
uchar lastTrg;
uchar cont;

void keyRead()
{
  uchar cur = 0,trg = 0;
  
  cur = (P3|0xF0) ^ 0xFF;
  trg = (cur ^ cont) & cur;
  cont = cur;
  
  if((lastTrg ^ trg & lastTrg) && cur)
  {
    //s4
    if(cur & 0x08)
    {
    }
  }
  
  lastTrg = trg;
}

void keyProc()
{
  if(keyCount > 10)
  {
    keyCount = 0;
    keyRead();
  }
}

void Timer2_Init(void)		//1毫秒@11.0592MHz
{
	AUXR |= 0x04;			//定时器时钟1T模式
	T2L = 0xCD;				//设置定时初始值
	T2H = 0xD4;				//设置定时初始值
	AUXR |= 0x10;			//定时器2开始计时
	IE2 |= 0x04;			//使能定时器2中断
}


void AllInit()
{
  P0 = 0xFF,P2 = 0x80,P2 = 0x00;
  P0 = 0x00,P2 = 0xA0,P2 = 0x00;
  Timer2_Init();
  EA = 1;
}

int main()
{
  AllInit();
  for(;;)
  {
    keyProc();
  }
}

void Timer2_Isr(void) interrupt 12
{
  //seg
  static segBit = 1;
  if(++segBit == 9) segBit =1;
  P0 = 0x00,P2 = 0xC0,P2 = 0x00;
  P0 = segDisp[segBit],P2 = 0xE0,P2 = 0x00;
  P0 = 1 << (segBit-1),P2 = 0xC0,P2 = 0x00;
  
  //key
  ++keyCount;
  
  //led
  P0 = ~LedDisp,P2 = 0x80,P2 = 0x00;
  
}
